package org.istt.domain;

import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

/**
 * WorkflowProcess entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "TBL_WORKFLOW_PROCESS", schema = "dbo", catalog = "onegate", uniqueConstraints=@UniqueConstraint(columnNames={"WORKFLOW_ID", "PROCESS_STATUS_ID", "FILE_STATUS_ID"}))
public class WorkflowProcess extends BaseObject<Long> implements java.io.Serializable {
	// Fields

	/**
	 * 
	 */
	private static final long serialVersionUID = 7641090735085659657L;
	public static final Integer START_POSITION = 1;
	private Long id;
	private ProcessStatus processStatus;
	private Workflow workflow;
	private Integer position;
	private Float processTime;
	private FileStatus fileStatus;
	private Boolean lineDept;
	private Boolean endProcess;
	
	/*User is processing this step*/
	private User userProcess;
	
	// Constructors

	/** default constructor */
	public WorkflowProcess() {
	}


	// Property accessors
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "ID", unique = true, nullable = false)
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "PROCESS_STATUS_ID")
	public ProcessStatus getProcessStatus() {
		return this.processStatus;
	}

	public void setProcessStatus(ProcessStatus processStatus) {
		this.processStatus = processStatus;
	}

	@ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
	@JoinColumn(name = "WORKFLOW_ID")
	public Workflow getWorkflow() {
		return this.workflow;
	}

	public void setWorkflow(Workflow workflow) {
		this.workflow = workflow;
	}
	
	@Column(name = "POSITION")
	public Integer getPosition() {
		return this.position;
	}

	public void setPosition(Integer position) {
		this.position = position;
	}

	@Column(name = "PROCESS_TIME", columnDefinition="float default 0")
	public Float getProcessTime() {
		return processTime;
	}

	public void setProcessTime(Float processTime) {
		this.processTime = processTime;
	}
	
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "FILE_STATUS_ID")
	public FileStatus getFileStatus() {
		return fileStatus;
	}

	public void setFileStatus(FileStatus fileStatus) {
		this.fileStatus = fileStatus;
	}
	
	@Column(name = "LINE_DEPARTMENT")
	public Boolean getLineDept() {
		return lineDept;
	}

	public void setLineDept(Boolean lineDept) {
		this.lineDept = lineDept;
	}
	
	@Transient
	public User getUserProcess() {
		return userProcess;
	}

	public void setUserProcess(User userProcess) {
		this.userProcess = userProcess;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (!(o instanceof WorkflowProcess)) return false;

		final WorkflowProcess wp = (WorkflowProcess) o;
		
		if (!getWorkflow().getId().equals(wp.getWorkflow().getId())) return false;
		if (!getProcessStatus().getId().equals(wp.getProcessStatus().getId())) return false;
			
		/*if (! getProcessStatus().equals(wp.getProcessStatus())) return false;
        if (! getWorkflow().equals(wp.getWorkflow())) return false;
		
        if (! (getPosition().equals(wp.getPosition())) ) return false;
        if (! (getProcessTime().equals(wp.getProcessTime())) ) return false;
        if (! (getFileStatus().equals(wp.getFileStatus())) ) return false;
        if (! (getLineDept().equals(wp.getLineDept())) ) return false;*/

		return true;
	}
	
	@Override
	public int hashCode() {
		int result;
		
		result = getWorkflow().hashCode();
		result = 29 * result + getProcessStatus().hashCode();
		
		/*result = getPosition().hashCode();
		result = 29 * result + (getProcessStatus() == null ? 0 : getProcessStatus().hashCode());
		result = 29 * result + (getWorkflow() == null ? 0 : getWorkflow().hashCode());
		result = 29 * result + (getPosition() == null ? 0 : getPosition().hashCode());
		result = 29 * result + (getProcessTime() == null ? 0 : getProcessTime().hashCode());
		result = 29 * result + (getFileStatus() == null ? 0 : getFileStatus().hashCode());
		result = 29 * result + (getLineDept() == null ? 0 : getLineDept().hashCode());*/
		return result;
	}

	@Override
	public String toString() {
		return new StringBuilder("id = ").append(id)
					.append(", position = ").append(position)
					.append(", processTime = ").append(processTime)
					.append(", lineDept = ").append(lineDept)
					.toString();
	}


	public void setEndProcess(Boolean endProcess) {
		this.endProcess = endProcess;
	}


	@Column(name = "END_POINT")
	public Boolean getEndProcess() {
		return endProcess;
	}

}